package com.itoffer.web;

import com.itoffer.pojo.Applicant;
import com.itoffer.service.ApplicantService;
import com.itoffer.service.impl.ApplicantServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/account/*")
public class AccountServlet extends BaseServlet {
    private ApplicantService applicantService = new ApplicantServiceImpl();
    /**
     * 退出登录方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void loginOutServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        String contextPath = req.getContextPath();
        resp.sendRedirect(contextPath+"/index.jsp");
    }

    /**
     * 修改密码方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void changePasswordServlet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
            String Password = req.getParameter("Password");
            //System.out.println(Password);
            String ComparePassword = req.getParameter("ComparePassword");
            //System.out.println(ComparePassword);
            HttpSession session = req.getSession();
            Applicant applicant = (Applicant) session.getAttribute("applicant");
            //调用业务层修改密码方法
            boolean b = applicantService.updateApplicantPwd(ComparePassword,applicant.getApplicant_id());
            if(b){
                resp.setContentType("text/json;charset=utf8");
                resp.getWriter().write("successful");
                //System.out.println("成功");
            }else{
                resp.setContentType("text/json;charset=utf8");
                resp.getWriter().write("fail");
                //System.out.println("失败");

        }
    }

    /**
     * 检查密码是否有问题
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void checkPasswordServlet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        String oldPassword = req.getParameter("oldPassword");

        HttpSession session = req.getSession();
        Applicant applicant = (Applicant) session.getAttribute("applicant");

        if(!oldPassword.equals(applicant.getApplicant_pwd())) {
            resp.setContentType("text/json;charset=utf8");
            resp.getWriter().write("pwdfalse");
            //System.out.println("密码错误");
        }
    }
}
